//
//  NSDictionary+Extend.m
//  XLDoctor
//
//  Created by longma on 2018/12/27.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "UILabel+MJUI.h"

@implementation UILabel (IB)

@dynamic mj_titleColorName;
- (void)setMj_titleColorName:(NSString *)mj_titleColorName {
    self.textColor = [UIColor mj_colorName:mj_titleColorName];
}

@dynamic mj_fontStyleName;
- (void)setMj_fontStyleName:(NSString *)mj_fontStyleName {
    self.font = [UIFont fontWithName:mj_fontStyleName size:self.font.pointSize];
}

@dynamic mj_lineSpacing;
- (void)setMj_lineSpacing:(CGFloat)mj_lineSpacing  {
    [self mj_paragraphStyleWithLineSpacing:mj_lineSpacing];
}

@dynamic mj_boldSize;
- (void)setMj_boldSize:(CGFloat)mj_boldSize {
    self.font = [UIFont boldSystemFontOfSize:mj_boldSize];
    //    self.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:mj_boldSize];

}

@dynamic mj_systemSize;
- (void)setMj_systemSize:(CGFloat)mj_systemSize {
    self.font = [UIFont systemFontOfSize:mj_systemSize];

}

@dynamic mj_bebasSize;
- (void)setMj_bebasSize:(CGFloat)mj_bebasSize {
    self.font = [UIFont fontWithName:@"BEBAS" size:mj_bebasSize];
}

@dynamic mj_harmonyOSSize;
- (void)setMj_harmonyOSSize:(CGFloat)mj_harmonyOSSize {
    self.font = [UIFont fontWithName:@"HarmonyOS_Sans_Condensed_Bold" size:mj_harmonyOSSize];

}
@end


/// 语言
@implementation UILabel (Language)

@dynamic mj_localizable;
- (void)setMj_localizable:(NSString *)mj_localizable {
    
    MJWeakSelf
    [RACObserve(LanguageHelper.shared, language) subscribeNext:^(NSString *language) {
        weakSelf.text = MJUILocalizedString(mj_localizable);
    }];

}
@end


@implementation UILabel (MJUI)

/**
 *  @method 多行字符串,字符串后面显示不全会自动换行
 *
 *  @param lineSpacing  行空隙
 */
- (void)mj_paragraphStyleWithLineSpacing:(CGFloat)lineSpacing
{
    NSInteger textAlignment = self.textAlignment;

    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpacing];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
    self.attributedText = attributedString;
    self.lineBreakMode = NSLineBreakByCharWrapping;
    self.textAlignment = textAlignment;
}

- (void)mj_paragraphStyleWithArrString:(NSArray *)arrString font:(UIFont * _Nullable)font color:(UIColor * _Nullable)color lineSpacing:(CGFloat )lineSpacing{
    
    NSInteger textAlignment = self.textAlignment;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing ? lineSpacing: 4.0f;// 行间距
    NSDictionary *ats = @{NSParagraphStyleAttributeName : paragraphStyle};
    NSMutableAttributedString *paragraphString = [[NSMutableAttributedString alloc] initWithString:self.text attributes:ats];
    for(NSString *string in arrString)
    {
        if(!string){
            return;
        }
        if(font){
            [paragraphString addAttribute:NSFontAttributeName
                                    value:font
                                    range:[self.text rangeOfString:string]];
        }
        if(color){
            [paragraphString addAttribute:NSForegroundColorAttributeName
                                    value:color
                                    range:[self.text rangeOfString:string]];
        }
        
    }
    self.attributedText = paragraphString;
    self.textAlignment = textAlignment;
    
}

- (void)mj_paragraphStyleWithTargetText:(NSString *)targetText font:(UIFont * _Nullable)font color:(UIColor * _Nullable)color lineSpacing:(CGFloat )lineSpacing{
    
    NSInteger textAlignment = self.textAlignment;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing ? lineSpacing: 4.0f;// 行间距
    NSDictionary *ats = @{NSParagraphStyleAttributeName : paragraphStyle};
    NSMutableAttributedString *paragraphString = [[NSMutableAttributedString alloc] initWithString:self.text attributes:ats];
    
    NSString *text = self.text;
    NSError *error;
    NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:targetText
    options:NSRegularExpressionCaseInsensitive error:&error];
    
    if (!error) {
        
        NSArray *results = [regular matchesInString:self.text options:NSMatchingReportProgress range:NSMakeRange(0,
    text.length)];
        
        for (NSTextCheckingResult *result in results) {
            
//            NSLog(@">>>2:%@", NSStringFromRange(result.range));
            
            if(font){
                [paragraphString addAttribute:NSFontAttributeName
                                        value:font
                                        range:result.range];
            }
            if(color){
                
                [paragraphString addAttribute:NSForegroundColorAttributeName
                                        value:color
                                        range:result.range];
            }
        }
    }
    else {
        NSLog(@"%@", error);
    }
  
    self.attributedText = paragraphString;
    self.textAlignment = textAlignment;
}


- (void)mj_paragraphStyleWithArrString:(NSArray *)arrString font:(UIFont * _Nullable)font color:(UIColor * _Nullable)color lineSpacing:(CGFloat )lineSpacing verticalCenter:(BOOL)verticalCenter{
    
    NSInteger textAlignment = self.textAlignment;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing ? lineSpacing: 4.0f;// 行间距
    
    NSDictionary *ats = @{NSParagraphStyleAttributeName : paragraphStyle};
    
    NSMutableAttributedString *paragraphString = [[NSMutableAttributedString alloc] initWithString:self.text attributes:ats];
    
    for(NSString *string in arrString)
    {
        if(!string){
            return;
        }
        if(font){
            [paragraphString addAttribute:NSFontAttributeName
                                    value:font
                                    range:[self.text rangeOfString:string]];
        }
        if(color){
            [paragraphString addAttribute:NSForegroundColorAttributeName
                                    value:color
                                    range:[self.text rangeOfString:string]];
        }
        if(verticalCenter){
            CGFloat fontsize = self.font.pointSize - font.pointSize;
            [paragraphString addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * fontsize) range:[self.text rangeOfString:string]];
        }
        
    }
    self.attributedText = paragraphString;
    self.textAlignment = textAlignment;
    
}
- (void)mj_paragraphStyleWithLineSpacing:(CGFloat)lineSpacing wordSpace:(CGFloat)wordSpace
{
    NSInteger textAlignment = self.textAlignment;

    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpacing];
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
    [attributedString addAttribute:NSKernAttributeName value:@(wordSpace) range:NSMakeRange(0, [self.text length])];

    self.attributedText = attributedString;
    self.lineBreakMode = NSLineBreakByCharWrapping;
    self.textAlignment = textAlignment;

    //[self sizeToFit];
}
@end
